package com.example.maoyanmovie.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import androidx.appcompat.app.AppCompatActivity;
import com.example.maoyanmovie.databinding.ActivityProfileEditBinding;
import com.example.maoyanmovie.utils.Constants;
import com.example.maoyanmovie.utils.SPUtils;
import com.example.maoyanmovie.utils.ToastUtils;

public class ProfileEditActivity extends AppCompatActivity {

    private ActivityProfileEditBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityProfileEditBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
        loadUserInfo();
    }

    private void initView() {
        binding.ivBack.setOnClickListener(v -> finish());

        binding.tvSave.setOnClickListener(v -> saveUserInfo());
    }

    private void loadUserInfo() {
        String username = SPUtils.getString(Constants.SP_KEY_USER_NAME, "");
        String phone = SPUtils.getString(Constants.SP_KEY_USER_PHONE, "");

        binding.etUsername.setText(username);
        binding.tvPhone.setText(phone);
    }

    private void saveUserInfo() {
        String username = binding.etUsername.getText().toString().trim();

        if (TextUtils.isEmpty(username)) {
            ToastUtils.showShort(this, "请输入用户名");
            return;
        }

        SPUtils.putString(Constants.SP_KEY_USER_NAME, username);
        ToastUtils.showShort(this, "保存成功");
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
